package study

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

object wc {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local[*]") setAppName ("wc")
    val streamingContext = new StreamingContext(conf, Seconds(5))

    /**
     * spark03:9999 Wordcount
     */
    val data: ReceiverInputDStream[String] = streamingContext.socketTextStream("spark03", 9999)
    val result: DStream[(String, Int)] = data.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)

    result.print()

    streamingContext.start()
    streamingContext.awaitTermination()

    streamingContext.stop(stopSparkContext = true, stopGracefully = true)
  }

}
